// pages/my/index.js
import { getAllNews,searchNews} from "../../api/news";

Page({
  data: {
    
    newsItems: [
    ],
    categories: [],
    date:[],
    selectedDate: '',
    selectedCategory: '',
    searchText:'',
    getNews:[] // 保存一份数据，其它地方会使用到
  },

  onSearchInput(e) {
    const searchText = e.detail.value;
    console.log('输入的',searchText);
    if(searchText.length===0){
      this.setData({
        newsItems:this.data.getNews,
      });
    }
    this.setData({
      searchText: searchText,
    });
  },
  onSearchClick() {
    this.manNews();

  },
  onSearchConfirm() {
    this.manNews();
  },
  async manNews(){
    if (this.data.searchText) {
      // 发送搜索请求
      const res=await searchNews(this.data.searchText);
      if(res.code===200) {
        if(res.data.length===0) {
          wx.showToast({
            title: '暂无相关内容',
            icon: 'none',
          });
          return ;
        }
        this.setData({
          newsItems: res.data 
        });      }
    } else {
      this.getAllNotifice()

    }
  },
  onDateChange(e) {
    const selectedDate = this.data.date[e.detail.value];
    this.setData({
      selectedDate: selectedDate === '全部' ? '' : selectedDate, // 如果选择“全部”，则设为空字符串
    });
    this.filterNotifications(); // 调用过滤方法

  },

  onCategoryChange(e) {
    const selectedCategory = this.data.categories[e.detail.value];
    this.setData({
      selectedCategory: selectedCategory === '全部' ? '' : selectedCategory, // 如果选择“全部”，则设为空字符串
    });
    this.filterNotifications(); // 调用过滤方法

  },
  filterNotifications() {
    const { getNews, selectedDate, selectedCategory } = this.data;

    const filteredMessages = getNews.filter(notification => {
      const matchesDate = selectedDate ? notification.publishTime.startsWith(selectedDate) : true;
      const matchesCategory = selectedCategory ? notification.category === selectedCategory : true;
      return matchesDate && matchesCategory; // 符合日期和类别条件
    });

    // 更新过滤后的通知
    this.setData({
      newsItems: filteredMessages
    });
  },
  
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      });
    }
  },
  onLoad(){
    this.getAllNotifice()

  },
  async getAllNotifice(){
    const res = await getAllNews()
    let date=[]
    let category=[]
    let formattedMessages = res.data.map(message => {
      if (message.publishTime) {
        message.publishTime = message.publishTime.replace("T", " "); // 替换 "T" 为 " "
      }
      date.push(message.publishTime)
      category.push(message.category)
      
      return message;
    });
    console.log('date',date,category);
    let uniqueYears = ['全部',...new Set(date.map(date => date.split(" ")[0].split("-")[0]))];
   
      let category2=['全部', ...new Set(category)] // 提取类别并去重

    this.setData({
      newsItems: formattedMessages.reverse(), 
      date:uniqueYears,
      categories:category2,
      getNews:formattedMessages
    });
  },
 

  


  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      });
    }
  },
  // onEditClick(e) {
  //   const title = e.currentTarget.dataset.title;
  //   wx.navigateTo({
  //     url: `/pages/edit/index?title=${title}`, 
  //   });
  // },
  addNews(){
    
    wx.navigateTo({
      url: '/pages/manage_news_detail/index?isEdit=0' 
    })
  }
 
  
  

});
